﻿using System;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;
using WsValid;
using System.Web.BizCode;
using WorkTimePrj.Models;
using System.Collections.Generic;

namespace WorkTimePrj.Controllers
{
    //[OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
    public class ProjectController : Controller
    {
        private WorkTimePrjEntities3 db = new WorkTimePrjEntities3();

        //
        // GET: /Project/
        [WSAuth(Roles="ba144991-0307-4a52-b6e8-ca7b8fc3b5b9")]
        public ViewResult Index(int? id,string code,string display,string status,string pm)
        {
            ViewBag.pmSelectList = new SelectList(db.User.Where("it.State!=1").OrderBy(e => e.DisplayName), "DisplayName", "DisplayName",pm);
            ViewBag.projstatusSelectList = new SelectList(db.ProjectStatus.OrderBy(e => e.ID), "ProjectStatus1", "ProjectStatus1",status);
            if (code == null)
                code = "";
            if (display == null)
                display = "";
            if (status == null)
                status = "";
            if (pm == null)
                pm = "";
            ViewBag.code = code;
            ViewBag.display = display;
            ViewBag.status = status;
            ViewBag.pm = pm;
            

            IQueryable<Project> proList = (from u in db.Project
                                     where (code == string.Empty || (code != string.Empty && u.code.Contains(code)))
                                         && (display == string.Empty || (display != string.Empty && u.display.Contains(display)))
                                         && (status == string.Empty || (status != string.Empty && u.status == status))
                                         && (pm == string.Empty || (pm != string.Empty && u.PM == pm))
                                     select u);
            ViewBag.cnt = proList.Count();
            PagedList<Project> project = proList.OrderByDescending(e => e.createtime).ToPagedList(id ?? 1, 9);
            return View(project);
        }

        [HttpPost]
        [WSAuth(Roles = "ba144991-0307-4a52-b6e8-ca7b8fc3b5b9")]
        public ActionResult Index(int? id, FormCollection form)
        {
            string code = form["TBCode"];
            string display = form["TBDisplay"];
            string status = form["projstatusSelectList"];
            string pm = form["pmSelectList"];
            return RedirectToAction("Index", new {id=1,code=code,display=display,status=status,pm=pm});
        }
        //
        // GET: /Project/Details/5
        [WSAuth(Roles = "ba144991-0307-4a52-b6e8-ca7b8fc3b5b9")]
        public ViewResult Details(Guid id)
        {
            Project project = db.Project.Single(p => p.pid == id);
            return View(project);
        }

        //
        // GET: /Project/Create
        [WSAuth(Roles = "ba144991-0307-4a52-b6e8-ca7b8fc3b5b9")]
        public ActionResult Create(int? refresh)
        {
            Project project = new Project();
            project.creator = Consts.CurrentUser.Name;
            project.createtime = DateTime.Now;
            ViewBag.refresh = 1;
            if (refresh==1)
                this.ShowMessage("保存成功");
            return View(project);
        } 

        //
        // POST: /Project/Create

        [HttpPost]
        [WSAuth(Roles = "ba144991-0307-4a52-b6e8-ca7b8fc3b5b9")]
        public ActionResult Create(Project project)
        {
            if (ModelState.IsValid)
            {
                db.Project.AddObject(project);
                project.pid = Guid.NewGuid();
                project.creator = Consts.CurrentUser.Name;
                project.createtime = DateTime.Now;
                db.SaveChanges();
                //ViewBag.refresh = 1;
                //this.ShowMessage("保存成功");
                //return RedirectToAction("Index");
                //this.CloseWindow();
                return RedirectToAction("Create", new { refresh = 1 });
            }
            else
                return View(project);
        }
        
        //
        // GET: /Project/Edit/5
        [WSAuth(Roles = "ba144991-0307-4a52-b6e8-ca7b8fc3b5b9")]
        public ActionResult Edit(Guid id)
        {
            Project project = db.Project.Single(p => p.pid == id);
            project.updatetime = DateTime.Now;
            return View(project);
        }

        //
        // POST: /Project/Edit/5

        [HttpPost]
        [WSAuth(Roles = "ba144991-0307-4a52-b6e8-ca7b8fc3b5b9")]
        public ActionResult Edit(Project project)
        {
            if (ModelState.IsValid)
            {
                db.Project.Attach(project);
                db.ObjectStateManager.ChangeObjectState(project, EntityState.Modified);
                project.updatetime = DateTime.Now;
                ViewBag.refresh = 1;
                db.SaveChanges();
                this.ShowMessage("保存成功");
            }
            return View(project);
        }

        [AcceptVerbs(HttpVerbs.Delete)]
        [WSAuth(Roles = "ba144991-0307-4a52-b6e8-ca7b8fc3b5b9")]   //[AcceptVerbs(HttpVerbs.Delete)]
        public ActionResult Delete(Guid id)
        {            
            Project project = db.Project.Single(p => p.pid == id);
            db.Project.DeleteObject(project);
            db.SaveChanges();
            ViewBag.refresh = 1;
            //this.ShowMessage("删除成功");
            return Json(new { msg = "1" });
        }
        
        public JsonResult CheckPM()
        {
            string name = Request["PM"];
            int cnt=-1;
            try
            {
                cnt = db.User.Where(u => u.DisplayName.IndexOf(name)!=-1).Count();
            }
            catch
            {

            }
            if (cnt>0)
                return Json(true, JsonRequestBehavior.AllowGet);
            else
                return Json(false, JsonRequestBehavior.AllowGet);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}